<?php

class Payments_model extends Model {
	
	var $payment_id;             // primary key
	var $pawn_id;                // foreign key referencing the Pawn Id
	var $payment_amount;         // Decimal(5,2)
	var $payment_date;
	var $payment_category_id;    // foreign key of payment_categories table
		
	function Payments_model()
	{
		parent::Model();
	}
	
	// This function compares an array of data to the table fields, and deletes any items in the data
	// that are not a field in the table
	
	protected function filterInputArray($table, $data, $xss_clean = false, $caps = true){
      $fields = $this->db->list_fields($table);
      
      foreach ($data as $k => $v) {
         if(in_array($k, $fields) == false)
         {
            unset($data[$k]);
         } 
   	   else 
   		{
            if($xss_clean === true) $data[$k] = $this->input->xss_clean($v);
            if($caps === true) $data[$k] = strtoupper($v); 	// convert to CAPS
         }
        }
        return $data;
   }

	function general()
	{
		$dateJS = 
		
		// Set up the parameters for the form_helper function that displays the fields 
		// in the customer add and customer update view
		$data['fPaymentId']         = array('name'=>'payment_id','size'=>2 ); 
		$data['fPawnId']            = array('name'=>'pawn_id','size'=>4 ); 
		$data['fPaymentAmount']     = array('name'=>'payment_amount', 'size'=>4 ); 
		$data['fPaymentMonth']      = array('name'=>'month','size'=>1); 	                                
		$data['fPaymentDay']        = array('name'=>'day', 'size'=>1 );                             
		$data['fPaymentYear']       = array('name'=>'year','size'=>2 );                    
		
		// Get the options to poppulate the dropdown lookup fields
		$fPaymentCategories = $this->getPaymentCategories();

      // setup the lookup fields
      $data['fPaymentCategoryId']	= array('name'=>'payment_category_id', 'options' => $fPaymentCategories, 'select' =>'');
      
      // Retrieve the database fields
      $fields = $this->db->list_fields('payments');
      $fields[] = 'month';
      $fields[] = 'day';
      $fields[] = 'year';
      
      // Create an associative array of the table field names as key, and an empty value
      foreach ($fields as $k => $v)
         $empty_table[$v] = '';			
				
		// convert the array to objects and store it in $data['result']
		$data['result'] = parseArrayToObject($empty_table);	
		
		return $data;    	    
	}
	
	function get() {
		
		// If any of the following properties are set before payments_model->get() is called from the controller
		//  then we will include a where statement for each of the properties that have been set.
		if ($this->payment_id) {
            $this->db->where("payment_id", $this->payment_id);
      }
      if ($this->pawn_id) {
         $this->db->where("pawn_id", $this->pawn_id);
      }
        
		$this->db->select('*');
		$this->db->select('payment_categories.payment_category_description');

		$this->db->from('payments');
		$this->db->join('payment_categories','payments.payment_category_id = payment_categories.payment_category_id','left');
				
		$query = $this->db->get();
		
		// If you set Payments_model->payment_id from your controller, then there will only be one row to return.
      if ($this->payment_id) {
         return ($query->row());		// returns an object
      }
      else
        	return $query->result();	// returns an array of objects
	}
	
	function save($data)
	{
		$update = FALSE;
		
		// Allows you to add a new customer as well as update an existing customer
		$db_array = $this->filterInputArray('payments', $data);
		
		if ($db_array['payment_id'] != '')		// If payment_id field is not blank, then it's an update
		{
			$update = TRUE;
			$payment_id = $db_array['payment_id'];
			
		}
		
		unset($db_array['payment_id']);
		
		// If Payments_model->payment_id was set in the controller, then we will update an existing record.
       
      if ($update) {
         $this->db->where("payment_id", $payment_id);
         $this->db->update("payments", $db_array);
      }
      else {
         $this->db->insert("payments", $db_array);
      }
   }
	
	function delete($id)
	{
		// function deletes a payment record based on the Id value
		// As long as Payments_model->payment_id was set in the controller, we will delete the record.
      
      $this->payment_id = $id;
        
		if ($this->payment_id) {
            $this->db->where("payment_id", $this->payment_id);
            $this->db->delete("payments");
        }
		
	}
	
   // Lookup Functions
	
	function getPaymentCategories() {
			
      $query = $this->db->get('payment_categories');
      if ($query->num_rows() > 0) {
      				
      	$query_result = $query->result_array();
      	
      	foreach ($query_result as $row)
      	{
      		$id = $row['payment_category_id'];
      		$value = $row['payment_category_description'];
      		$options_array[$id] = $value;
      	}
      	return $options_array;
      }
      else
      	return FALSE;
	}
}

/** lookup code from internet -> this goes on the view page

  echo '<select name="source" id="source">';
  $result = mysql_query("SELECT * FROM source");
  while($row = mysql_fetch_array($result)){
    echo '<option value="'.$row['source_id'].'"';
    if($client_info['source_id'] == $row['source_id']) echo ' selected';
    echo '>'.$row['source'].'</option>';
  }
  echo '</select>';
 */

?>